package com.lyw.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 解决跨域问题
 *
 * @Auther liyawei
 * @Date 2022/6/4 13:52
 */

@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        // 跨域配置
        // 请求头
        corsConfiguration.addAllowedHeader("*");
        // 请求方式
        corsConfiguration.addAllowedMethod("*");
        // 请求源
        corsConfiguration.addAllowedOriginPattern("*");
        // 是否允许 cookie
        corsConfiguration.setAllowCredentials(true);

        // 任意路径都需要跨域配置，否则丢失 cookie 信息
        source.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsWebFilter(source);
    }
}
